package com.framework.modules.appindex;

import com.framework.entity.or.OrBanner;
import com.framework.entity.sys.SysDeptJumpUrl;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.sys.controller.AbstractController;
import com.framework.page.PageUtils;
import com.framework.service.BannerService;
import com.framework.service.SysDeptJumpUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * app首页轮播图相关
 * @author wq
 */
@RestController
@RequestMapping("/sys/orBanner")
public class OrBannerController extends AbstractController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private SysDeptJumpUrlService sysDeptJumpUrlService;

    /**
     * 轮播图列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public RespResult queryBannerList(@RequestParam Map<String, Object> params) {
        params.put("deptId", getUser().getDeptId());
        PageUtils page = bannerService.queryBannerListForAdmin(params);
        return RespResult.ok().put("page", page);
    }

    /**
     * 根据id查询
     *
     * @param bannerId
     * @return
     */
    @RequestMapping("/findById/{bannerId}")
    public RespResult queryById(@PathVariable Long bannerId) {
        OrBanner banner = bannerService.queryBannerInfoById(bannerId);
        return RespResult.ok().put("banner", banner);
    }

    /**
     * 修改轮播图
     *
     * @param banner
     * @return
     */
    @RequestMapping("/update")
    public RespResult updateBanner(@RequestBody OrBanner banner) {
        if(banner.getJumpType().intValue()==2){
            SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),1);
            banner.setJumpUrl(sysDeptJumpUrl.getBannerJump());
        }
        bannerService.updateById(banner);
        return RespResult.ok();
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadBanner")
    public RespResult uploadBanner(@RequestParam("file") MultipartFile file) throws IOException {
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),1);
        String prefix=sysDeptJumpUrl.getUploadUrl();
        String bannerUrl = "";
        String suffix;
        if (file != null) {
            //上传轮播图图
            suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            bannerUrl = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);
        }
        return RespResult.ok().put("url", bannerUrl);
    }

    /**
     * 添加轮播图
     * @param banner
     * @return
     */
    @RequestMapping("/insert")
    public RespResult insertBanner(@RequestBody OrBanner banner) {
        banner.setCreateDate(new Date());
        if(banner.getJumpType().intValue()==2){
            SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),1);
            banner.setJumpUrl(sysDeptJumpUrl.getBannerJump());
        }
        banner.setDeptId(getDeptId());
        bannerService.insert(banner);
        return RespResult.ok();
    }


}
